package com.clkj.aimin.controller;

import com.clkj.aimin.dao.user.UserCasesMapper;
import com.clkj.aimin.pojo.helper.HelperTalk;
import com.clkj.aimin.pojo.user.*;
import com.clkj.aimin.pojo.common.Result;
import com.clkj.aimin.pojo.common.StatusCode;
import com.clkj.aimin.service.user.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yawang
 * @create 2024/04/02 17:18:39
 **/
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(value = "/user", tags = {"用户信息api"})

public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "app---完善用户基本信息")
    @PostMapping("/updateUser")
    public Result updateUser(User user, HttpServletRequest request) {
        try {
            User users = userService.updateUser(user, request);
            return new Result(true, StatusCode.OK, "添加成功", users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

//    @ApiOperation(value = "添加用户问卷")
//    @PostMapping("/addHealthRecords")
//    public Result addHealthRecords(UserHealth health) {
//        try {
//            userService.addHealthRecords(health);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

//    @ApiOperation(value = "获取用户问卷")
//    @PostMapping("/getHealthRecord")
//    public Result getHealthRecord(Integer userId) {
//        try {
//            UserHealth health = userService.getHealthRecord(userId);
//            return new Result(true, StatusCode.OK, "添加成功", health);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

    @ApiOperation(value = "后台添加邀请码")
    @PostMapping("/addUserType")
    public Result addUserType(@ApiParam(name = "userType", value = "身份") @RequestParam("userType") Integer userType,
                              @ApiParam(name = "num", value = "生成数量") @RequestParam("num") Integer num) {
        try {
            userService.addUserType(userType, num);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "后台获取邀请码列表")
    @PostMapping("/listUserType")
    public Result listUserType() {
        try {
            List<UserType> userTypes = userService.listTypes();
            return new Result(true, StatusCode.OK, "添加成功", userTypes);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app---用户微信登录")
    @PostMapping("/login")
    public Result login(
                        @ApiParam(name = "unionId", value = "unionId") @RequestParam("unionId") String unionId,
                        @ApiParam(name = "openId", value = "openId") @RequestParam("openId") String openId,
                        @ApiParam(name = "userName", value = "用户信息") @RequestParam("userName") String userName,
                        @ApiParam(name = "userImg", value = "头像") @RequestParam("userImg") String userImg,
                        @ApiParam(name = "userSex", value = "性别") @RequestParam("userSex") String userSex) {
        try {
            Map map = userService.login(unionId,openId,userName,userImg,userSex);
            return new Result(true, StatusCode.OK, "添加成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app---任务:用户完成每日任务")
    @PostMapping("/updateUserTask")
    public Result updateUserTask(@ApiParam(name = "userId", value = "用户id") @RequestParam Integer userId,
                                 @ApiParam(name = "taskName", value = "任务名") @RequestParam String taskName,
                                 @ApiParam(name = "score", value = "任务积分数") @RequestParam Integer score) {
        try {
            Result result = userService.updateUserTask(userId, taskName, score);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Map map = new HashMap();
            map.put("flag", 0);
            return new Result(false, StatusCode.ERROR, "添加失败",map);
        }
    }
    @ApiOperation(value = "app=----任务:获取用户今日完成的任务")
    @PostMapping("/getUserTask")
    public Result getUserTask(@ApiParam(name = "userId", value = "用户id") @RequestParam Integer userId) {
        try {
            Set set = userService.getUserTask(userId);
            return new Result(true, StatusCode.OK, "添加成功", set);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "app--获取任务详情")
    @PostMapping("/getUserTaskInfo")
    public Result getUserTaskInfo(Integer userId) {
        try {
            List<TaskInfo> list = userService.getUserTaskInfo(userId);
            return new Result(true, StatusCode.OK, "添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app---病例:用户上传病例和检查报告")
    @PostMapping("/addUserCases")
    public Result addUserCases(@ApiParam(name = "userId", value = "用户id") @RequestParam Integer userId, HttpServletRequest request) {
        try {
            userService.addUserCases(userId, request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app---病例:获取用户病例和检查报告")
    @PostMapping("/getUserCases")
    public Result getUserCases(@ApiParam(name = "userId", value = "用户id") @RequestParam Integer userId, @ApiParam(name = "type", value = "0获取病例 1获取报告 2获取全部") @RequestParam Integer type) {
        try {
            List<UserCases> map = userService.getUserCases(userId, type);
            return new Result(true, StatusCode.OK, "添加成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

//    @ApiOperation(value = "病例:批量删除用户病例和检查报告")
//    @PostMapping("/deleteUserCases")
//    public Result deleteUserCases(@ApiParam(name = "userId", value = "用户id") @RequestParam Integer userId, @ApiParam(name = "ids", value = "图片id") @RequestParam String ids) {
//        try {
//            userService.deleteUserCases(userId, ids);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

    @ApiOperation(value = "app--根据家庭码新增家庭团队成员")
    @PostMapping("/addUserByFamilyCode")
    public Result addUserByFamilyCode(@ApiParam(name = "familyCode", value = "主账户家庭码") @RequestParam String familyCode,
                                      @ApiParam(name = "userId", value = "被添加人用户id") @RequestParam String userId){
        try {
            Result result = userService.addUserByFamilyCode(familyCode,userId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "app--家庭有次数，申请加号和亲情服务")
    @PostMapping("/userAddOrder")
    public Result userAddOrder(UserOrder userOrder,HttpServletRequest request){
        try {
            Map  result = userService.userAddOrder(userOrder,request);
            return new Result(true, StatusCode.OK, "添加成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
//    @ApiOperation(value = "聊天:用户点击客服和管理员沟通")
//    @PostMapping("/addHelperTalk")
//    public Result addHelperTalk(Integer userId,Integer helpId,String content){
//        try {
//            userService.addHelperTalk(userId,helpId,content);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
//    @ApiOperation(value = "聊天:用户点击客服获取和管理员的沟通记录")
//    @PostMapping("/getHelperTalkInfo")
//    public Result getHelperTalkInfo(Integer userId,Integer helper){
//        try {
//            List<HelperTalk> list = userService.getDoctorInfo(userId,helper);
//            return new Result(true, StatusCode.OK, "添加成功",list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
//    @ApiOperation(value = "聊天:获取用户未读消息个数")
//    @PostMapping("/getUserUnRead")
//    public Result getUserUnRead(Integer userId){
//        try {
//            Integer num = userService.getUserUnRead(userId);
//            return new Result(true, StatusCode.OK, "添加成功",num);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
    @ApiOperation(value = "app---开通家庭服务套餐或加号、亲情服务购买")
    @PostMapping("/addUserOrder")
    public Result addUserOrder(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId") Integer userId,
                               @ApiParam(name = "rechargeMoney", value = "充值金额") @RequestParam("rechargeMoney") String rechargeMoney,
                               @ApiParam(name = "doctorId", value = "医生id") @RequestParam("doctorId") Integer doctorId,
                               @ApiParam(name = "helperId", value = "助理id") @RequestParam("helperId") Integer helperId,
                               @ApiParam(name = "flag", value = "0加号 1亲情就诊 2 开通家庭套餐") @RequestParam("flag") Integer flag,
                               @ApiParam(name = "applyId", value = "如果不是开通，选择审核人,否则传自己id") @RequestParam("applyId") Integer applyId,
                               @ApiParam(name = "parentId", value = "家长id") @RequestParam("parentId") Integer parentId,
                               HttpServletRequest request){
        try {
            Map result = userService.addUserOrder(userId,rechargeMoney,doctorId,helperId,flag,applyId,parentId,request);
            return new Result(true, StatusCode.OK, "成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app---开通家庭服务套餐或加号、亲情服务购买测试")
    @PostMapping("/payTest")
    public Result payTest(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId") Integer userId,
                               @ApiParam(name = "rechargeMoney", value = "充值金额") @RequestParam("rechargeMoney") String rechargeMoney,
                               @ApiParam(name = "doctorId", value = "医生id") @RequestParam("doctorId") Integer doctorId,
                               @ApiParam(name = "helperId", value = "助理id") @RequestParam("helperId") Integer helperId,
                               @ApiParam(name = "flag", value = "0加号 1亲情就诊 2 开通家庭套餐") @RequestParam("flag") Integer flag,
                               @ApiParam(name = "applyId", value = "如果不是开通，选择审核人,否则传自己id") @RequestParam("applyId") Integer applyId,
                          @ApiParam(name = "parentId", value = "家长id") @RequestParam("parentId") Integer parentId,HttpServletRequest request){
        try {
            userService.payTest(userId,rechargeMoney,doctorId,helperId,flag,applyId,parentId,request);
            return new Result(true, StatusCode.OK, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "app--开通家庭服务套餐回调")
    @PostMapping("/paySuccess")
    public Result paySuccess(HttpServletRequest request, HttpServletResponse response){
        try {
            userService.paySuccess(request,response);
            return new Result(true, StatusCode.OK,"充值成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }



    @ApiOperation(value = "app---获取家庭加号，亲情服务使用详情")
    @PostMapping("/getUserOrderDetail")
    public Result getUserOrderDetail(Integer userId,String parentCode){
        try {
            List<UserOrder> list = userService.getUserOrderDetail(userId,parentCode);
            return new Result(true, StatusCode.OK, "获取成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }
    @ApiOperation(value = "app--获取我的家庭信息")
    @PostMapping("/getUserInfoById")
    public Result getUserInfoById(Integer userId){
        try {
            Map user = userService.getUserInfoById(userId);
            return new Result(true, StatusCode.OK, "获取成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app---手机号登录")
    @PostMapping("/telLogin")
    public Result telLogin(String tel,String password){
        try {
            Map user = userService.telLogin(tel,password);
            return new Result(true, StatusCode.OK, "获取成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app--创建家庭生成邀请码")
    @PostMapping("/creatCode")
    public Result creatCode(Integer userId){
        try {
            String code  = userService.creatCode(userId);
            return new Result(true, StatusCode.OK, "获取成功",code);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app---获取家庭剩余加号/亲情服务次数")
    @PostMapping("/listFamilyNum")
    public Result listFamilyNum(@ApiParam(name = "userId", value = "用户id") @RequestParam("userId") Integer userId,
                            @ApiParam(name = "parentCode", value = "家庭码,没有null") @RequestParam("parentCode") String parentCode){
        try {
            List<User> users   = userService.listFamilyNum(userId,parentCode);
            return new Result(true, StatusCode.OK, "获取成功",users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app--普通用户获取我的审核列表")
    @PostMapping("/listApplyList")
    public Result listApplyList(@ApiParam(name = "userId", value = "用户id") @RequestParam("userId") Integer userId){
        try {
            List<UserOrder> users   = userService.listApplyList(userId);
            return new Result(true, StatusCode.OK, "获取成功",users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app--普通用户审核申请列表")
    @PostMapping("/applyOrder")
    public Result applyOrder(@ApiParam(name = "orderId", value = "申请列表id") @RequestParam("orderId") Integer orderId,
                             @ApiParam(name = "status", value = "审核状态0不通过 1通过") @RequestParam("status") Integer status,
                             @ApiParam(name = "note", value = "驳回原因") @RequestParam("note") String  note){
        try {
            userService.applyOrder(orderId,status,note);
            return new Result(true, StatusCode.OK, "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }
    @ApiOperation(value = "app--个人中心获取我的医生和助理")
    @PostMapping("/geren")
    public Result geren(@ApiParam(name = "userId", value = "用户id") @RequestParam("userId") Integer userId){
        try {
            User user = userService.geren(userId);
            return new Result(true, StatusCode.OK, "获取成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }


    @ApiOperation(value = "app--个人中心我的二维码")
    @PostMapping("/erweima")
    public Result erweima(@ApiParam(name = "doctorId", value = "医生id") @RequestParam("doctorId") Integer doctorId,
                          @ApiParam(name = "helperId", value = "医助id") @RequestParam("helperId") Integer helperId){
        try {
            String erweima =  userService.erweima(doctorId,helperId);
            return new Result(true, StatusCode.OK, "获取成功",erweima);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "weixin--用户扫描二维码")
    @PostMapping("/scanMa")
    public Result scanMa(@ApiParam(name = "code", value = "code") @RequestParam("code") String  code,
                        @ApiParam(name = "doctorId", value = "医生id") @RequestParam("doctorId") Integer doctorId,
                          @ApiParam(name = "helperId", value = "医助id") @RequestParam("helperId") Integer helperId){
        try {
             userService.scanMa(code,doctorId,helperId);
            return new Result(true, StatusCode.OK, "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "后台用户列表")
    @PostMapping("/listUser")
    public Result listUser(Integer page,Integer size,String userName,Integer doctorId,Integer helperId,Integer userType){
        try {
            PageInfo userList = userService.listUser(page,size,userName,doctorId,helperId,userType);
            return new Result(true, StatusCode.OK, "获取成功",userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app---用户删除档案")
    @PostMapping("/deleteHealth")
    public Result deleteHealth(Integer id){
        try {
            userService.deleteHealth(id);
            return new Result(true, StatusCode.OK, "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app---亲情服务驳回,再次申请")
    @PostMapping("/unApplyService")
    public Result unApplyService(Integer orderId,String orderNumber,HttpServletRequest request){
        try {
            userService.unApplyService(orderId,orderNumber,request);
            return new Result(true, StatusCode.OK, "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    @ApiOperation(value = "app---我的申请列表（用户）")
    @PostMapping("/listMyApply")
    public Result listMyApply(Integer userId){
        try {
            List<UserOrder> orderList = userService.listMyApply(userId);
            return new Result(true, StatusCode.OK, "获取成功",orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }


    @ApiOperation(value = "获取最新版本")
    @PostMapping("/listNewVer")
    public Result listNewVer() {
        try {
            List<Version> vers = userService.listNewVer();
            return new Result(true, StatusCode.OK, "操作成功", vers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "获取App版本更新记录")
    @PostMapping("/listVer")
    public Result listVer() {
        try {
            List<Version> vers = userService.listVer();
            return new Result(true, StatusCode.OK, "操作成功", vers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "测试添加版本")
    @PostMapping("/addVer")
    public Result addVer(Version version){
        try {
            userService.addVer(version);
            return new Result(true, StatusCode.OK, "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }
}


